package com.todo.strategy.offline.application.backend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.BizPageResponse;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.strategy.common.enums.PlanRelationSceneEnum;
import com.todo.strategy.common.enums.StrategyStatusEnum;
import com.todo.strategy.offline.application.backend.service.PlatformStrategyBackendService;
import com.todo.strategy.offline.application.convert.StrategyConvert;
import com.todo.strategy.offline.application.entity.bo.PlanRelationBO;
import com.todo.strategy.offline.application.entity.bo.PlatformStrategyBO;
import com.todo.strategy.offline.application.entity.bo.PlatformStrategyDetailBO;
import com.todo.strategy.offline.application.entity.bo.PlatformStrategyExtendBO;
import com.todo.strategy.offline.application.entity.condition.PlatformStrategyConditionBO;
import com.todo.strategy.offline.application.service.PlanRelationService;
import com.todo.strategy.offline.application.service.PlatformStrategyService;
import com.todo.strategy.offline.iface.request.*;
import com.todo.strategy.offline.iface.response.PlatformStrategyDetailResp;
import com.todo.strategy.offline.iface.response.PlatformStrategyRelationItemResp;
import com.todo.strategy.offline.iface.response.PlatformStrategyRelationResp;
import com.todo.strategy.offline.iface.response.QueryPlatformStrategyByConditionResp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (PlatformStrategy)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:47
 */
@Slf4j
@Service("platformStrategyBackendService")
public class PlatformStrategyBackendServiceImpl implements PlatformStrategyBackendService {

    @Resource
    private PlatformStrategyService platformStrategyService;
    @Resource
    private PlanRelationService planRelationService;

    @Override
    public BizResponse<Boolean> enablePlatformStrategyById(EnablePlatformStrategyByIdReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        StrategyStatusEnum statusEnum = StrategyStatusEnum.PUBLISHED;
        if (!req.getEnable()) {
            statusEnum = StrategyStatusEnum.DISABLE;
        }
        Boolean result = platformStrategyService.updatePlatformStrategyStatus(Long.valueOf(req.getStrategyId()), statusEnum);
        return BizResponse.success(result);
    }

    @Override
    public BizResponse<PlatformStrategyDetailResp> queryPlatformStrategyDetailById(QueryPlatformStrategyDetailByIdReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        PlatformStrategyDetailBO platformStrategyDetailBO = platformStrategyService.queryPlatformStrategyDetailById(req.getStrategyId());

        return BizResponse.success(StrategyConvert.convertToPlatformStrategyDetailResp(platformStrategyDetailBO));
    }

    @Override
    public BizPageResponse<QueryPlatformStrategyByConditionResp> queryPlatformStrategyByCondition(QueryPlatformStrategyByConditionReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        PlatformStrategyConditionBO conditionBO = StrategyConvert.convertToPlatformStrategyConditionBO(req);
        PageResult<PlatformStrategyBO> pageResult = platformStrategyService.queryPlatformStrategyByCondition(conditionBO);
        if (pageResult == null) {
            return BizPageResponse.systemError("操作异常，请联系开发确认");
        }
        return BizPageResponse.success(StrategyConvert.convertToQueryPlatformStrategyByConditionRespPage(pageResult));
    }

    @Override
    public BizResponse<Boolean> insertPlatformStrategy(InsertPlatformStrategyReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        PlatformStrategyBO platformStrategyBO = StrategyConvert.convertToPlatformStrategyBO(req);
        Boolean insertResult = platformStrategyService.insertPlatformStrategyInfo(platformStrategyBO);
        log.info("PlatformStrategyBackendService#insertPlatformStrategy 新增平台攻略完成 入参：【{}】，结果：【{}】", JSONObject.toJSONString(platformStrategyBO), insertResult);
        return BizResponse.success(insertResult);
    }

    @Override
    public BizResponse<Boolean> updatePlatformStrategy(UpdatePlatformStrategyReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        PlatformStrategyBO platformStrategyBO = StrategyConvert.convertToPlatformStrategyBO(req);
        Boolean updateResult = platformStrategyService.updatePlatformStrategyInfo(platformStrategyBO);
        log.info("PlatformStrategyBackendService#updatePlatformStrategy 更新平台攻略完成 入参：【{}】，结果：【{}】", JSONObject.toJSONString(platformStrategyBO), updateResult);
        return BizResponse.success(updateResult);
    }

    @Override
    public BizResponse<Boolean> appendPlatformStrategyDetail(AppendPlatformStrategyDetailReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        PlatformStrategyExtendBO alreadyData = platformStrategyService.queryPlatformStrategyExtendById(Long.valueOf(req.getStrategyId()));
        Boolean appendResult = false;
        PlatformStrategyExtendBO platformStrategyBO = StrategyConvert.convertToPlatformStrategyExtendBO(req);
        if (alreadyData == null) {
            //如果为空则新增
            appendResult = platformStrategyService.insertPlatformStrategyExtend(platformStrategyBO);
        } else {
            appendResult = platformStrategyService.updatePlatformStrategyExtend(platformStrategyBO);
        }
        log.info("PlatformStrategyBackendService#appendPlatformStrategyDetail 追加平台攻略价值完成 入参：【{}】，结果：【{}】", JSONObject.toJSONString(platformStrategyBO), appendResult);
        return BizResponse.success(appendResult);
    }

    @Override
    public BizResponse<Boolean> insertStrategyRelation(InsertStrategyRelationReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        PlanRelationBO planRelationBO = StrategyConvert.convertToPlanRelationBO(req);
        Boolean insertResult = planRelationService.insertPlanRelation(planRelationBO);
        log.info("PlatformStrategyBackendService#insertStrategyRelation 新增平台攻略关联信息完成 入参：【{}】，结果：【{}】", JSONObject.toJSONString(planRelationBO), insertResult);
        return BizResponse.success(insertResult);
    }

    @Override
    public BizResponse<Boolean> updateStrategyRelation(UpdateStrategyRelationReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        PlanRelationBO planRelationBO = StrategyConvert.convertToPlanRelationBO(req);
        Boolean updateResult = planRelationService.updatePlanRelation(planRelationBO);
        log.info("PlatformStrategyBackendService#updateStrategyRelation 更新平台攻略关联信息完成 入参：【{}】，结果：【{}】", JSONObject.toJSONString(planRelationBO), updateResult);
        return BizResponse.success(updateResult);
    }

    @Override
    public BizResponse<List<PlatformStrategyRelationResp>> queryStrategyRelationList(QueryStrategyRelationListReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        List<PlanRelationBO> planRelationBOS = planRelationService.queryPlanRelationByBizId(req.getStrategyId(), PlanRelationSceneEnum.PLATFORM_STRATEGY);
        if (CollectionUtils.isEmpty(planRelationBOS)) {
            log.info("PlatformStrategyBackendService#queryStrategyRelationList 获取平台攻略关联信息完成，数据为空  入参：【{}】", JSONObject.toJSONString(req));
            return BizResponse.success();
        }
        return BizResponse.success(StrategyConvert.convertToPlatformStrategyRelationRespList(planRelationBOS));
    }

    @Override
    public BizResponse<Boolean> detectionPlatformStrategyById(DetectionPlatformStrategyReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        PlatformStrategyBO platformStrategyBO = platformStrategyService.queryPlatformStrategyInfoById(req.getPlatformStrategyId());
        if (platformStrategyBO == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        if (StrategyStatusEnum.DISABLE.equals(platformStrategyBO.getStatus())) {
            log.warn("PlatformStrategyBackendService#detectionPlatformStrategyById 检测平台攻略完成，该攻略已禁用，无法操作 req：【{}】", JSONObject.toJSONString(req));
            return BizResponse.operationFailed("该攻略已禁用，无法操作");
        }
        if (StrategyStatusEnum.AUTO_EXAMINE_FAILED.equals(platformStrategyBO.getStatus())) {
            log.warn("PlatformStrategyBackendService#detectionPlatformStrategyById 检测平台攻略完成，当前数据为非自动检测失败状态，无法操作 req：【{}】", JSONObject.toJSONString(req));
            return BizResponse.operationFailed("当前数据为非自动检测失败状态，无法操作");
        }
        Boolean checkResult;
        if (req.getPass()) {
            checkResult = platformStrategyService.updatePlatformStrategyStatus(req.getPlatformStrategyId(), StrategyStatusEnum.PUBLISHED);
        } else {
            checkResult = platformStrategyService.updatePlatformStrategyStatus(req.getPlatformStrategyId(), StrategyStatusEnum.EXAMINE_FAILED);
        }
        log.info("UserStrategyBackendService#detectionUserStrategyById 检测用户攻略完成，检测结果为：【{}】 req：【{}】", checkResult, JSONObject.toJSONString(req));
        return BizResponse.success(checkResult);
    }

    @Override
    public BizResponse<Boolean> removeStrategyRelation(RemoveStrategyRelationReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        Boolean removeResult = planRelationService.removePlanRelationById(req.getId());
        log.info("PlatformStrategyBackendService#removeStrategyRelation 删除平台攻略关联信息完成 入参：【{}】，结果：【{}】", JSONObject.toJSONString(req), removeResult);
        return BizResponse.success(removeResult);
    }

    @Override
    public BizResponse<PlatformStrategyRelationItemResp> queryStrategyRelationById(QueryStrategyRelationByIdReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        PlanRelationBO planRelationBO = planRelationService.queryPlanRelationById(req.getId());
        if (planRelationBO == null) {
            log.info("PlatformStrategyBackendService#queryStrategyRelationById 获取平台攻略关联信息完成，数据为空  入参：【{}】", JSONObject.toJSONString(req));
            return BizResponse.success();
        }
        return BizResponse.success(StrategyConvert.convertToPlatformStrategyRelationItemResp(planRelationBO));
    }
}
